package main

import (
	"fmt"
	"demserver/src/elevation"
)

func main() {
	// Create a GeoTIFF struct with actual values from our TFW file
	geoTiff := &elevation.GeoTIFF{
		Width:  20000, // Approximate size - we'll adjust this
		Height: 20000, // Approximate size - we'll adjust this
	}
	
	// Set the geotransform parameters from the TFW file
	geoTiff.GeoTransform[0] = 102.33175524278985  // GT(0) - x-coordinate of the center of the upper left pixel
	geoTiff.GeoTransform[1] = 0.000130244367997  // GT(1) - pixel size in the x-direction
	geoTiff.GeoTransform[2] = 0.0                  // GT(2) - rotation about x-axis
	geoTiff.GeoTransform[3] = 31.768073065845446 // GT(3) - y-coordinate of the center of the upper left pixel
	geoTiff.GeoTransform[4] = 0.0                  // GT(4) - rotation about y-axis
	geoTiff.GeoTransform[5] = -0.000130250355006 // GT(5) - pixel size in the y-direction
	
	// Test coordinate conversion with your coordinates
	lat := 31.029596331678416
	lon := 103.55108474360813
	x, y, err := geoTiff.LatLonToPixel(lat, lon)
	if err != nil {
		fmt.Printf("Failed to convert coordinates (%f, %f): %v\n", lat, lon, err)
		return
	}
	fmt.Printf("Pixel coordinates for (%f, %f): (%f, %f)\n", lat, lon, x, y)
	
	// Calculate corner coordinates
	upperLeftLat := geoTiff.GeoTransform[3]  // GT(3)
	upperLeftLon := geoTiff.GeoTransform[0]  // GT(0)
	
	fmt.Printf("Upper left corner (lat, lon): (%f, %f)\n", upperLeftLat, upperLeftLon)
	
	lowerRightLat := geoTiff.GeoTransform[3] + geoTiff.GeoTransform[5]*float64(geoTiff.Height)
	lowerRightLon := geoTiff.GeoTransform[0] + geoTiff.GeoTransform[1]*float64(geoTiff.Width)
	
	fmt.Printf("Lower right corner (lat, lon): (%f, %f)\n", lowerRightLat, lowerRightLon)
	
	// Check if our test coordinates are within the bounds
	fmt.Printf("Test coordinates: lat=%f, lon=%f\n", lat, lon)
	if lat <= upperLeftLat && lat >= lowerRightLat && lon >= upperLeftLon && lon <= lowerRightLon {
		fmt.Printf("Test coordinates are within the geographic bounds\n")
		fmt.Printf("Coordinates are within image bounds: %t\n", x >= 0 && y >= 0 && x < float64(geoTiff.Width) && y < float64(geoTiff.Height))
	} else {
		fmt.Printf("Test coordinates are outside the geographic bounds\n")
		fmt.Printf("Lat bounds: %f to %f\n", lowerRightLat, upperLeftLat)
		fmt.Printf("Lon bounds: %f to %f\n", upperLeftLon, lowerRightLon)
	}
}
